<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!--    导入自己的css文件-->
    <link rel="stylesheet" href="../css/my-css.css">
    <style>
        .el-main {
            width: 1200px;
            margin: 0 auto;
        }

        .reg_form {
            width: 375px;
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <my-header></my-header>
        <el-main style="margin-top: 30px">
            <el-row gutter="20">
                <el-col span="12">
                    <el-card>
                        <img src="../img/usermodel/reg.png" width="100%" height="650px">
                    </el-card>
                </el-col>
                <el-col span="12">
                    <!--注册表单开始-->
                    <el-form :model="user" :rules="rules" ref="user" label-width="80px" style="margin-top: 100px">
                        <el-form-item>
                            <h1 style="float: left;font-size: 28px">立即注册</h1>
                            <a style="float: right;
                            text-decoration: none;color: #409EFF;
                            position: relative;top: 35px" href="login.html">
                                已有账号?现在登录
                            </a>
                        </el-form-item>
                        <el-form-item prop="username" label="用户名">
                            <el-input v-model="user.username" placeholder="请输入用户名"
                                      class="reg_form">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password" label="密码">
                            <el-input type="password" v-model="user.password"
                                      placeholder="请输入密码" class="reg_form">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="nickname" label="昵称">
                            <el-input v-model="user.nickname" placeholder="请输入昵称" class="reg_form"></el-input>
                        </el-form-item>

                        <el-radio style="margin-left: 75px" v-model="user.gender" label="0">男</el-radio>
                        <el-radio v-model="user.gender" label="1">女</el-radio>


                        <el-form-item style="margin-top: 30px">
                            <el-button type="primary" @click="reg('user')">注册</el-button>
                        </el-form-item>
                    </el-form>
                    <!--注册表单结束-->
                </el-col>
            </el-row>

        </el-main>
        <my-footer></my-footer>
    </el-container>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入axios框架文件-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<!--导入自己的头部js文件-->
<script src="../js/my-header.js"></script>
<!--导入自己的尾部js文件-->
<script src="../js/my-footer.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                user: {
                    username: "",
                    password: "",
                    nickname: "",
                    gender: "0",
                },
                //表单验证规则
                rules: {
                    username: [
                        {validator: this.checkUserName, trigger: 'blur'}
                    ],
                    password: [
                        {validator: this.checkPassword, trigger: 'blur'}
                    ],
                    nickname: [
                        {validator: this.checkNickname, trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            //校验用户名
            checkUserName(rule, value, callback) {
                if (value == '') {
                    callback(new Error('用户名不能为空'));
                } else if (value.length > 6) {
                    callback(new Error('用户名的长度不能超过6位'))
                } else {
                    //判断用户是否存在
                    axios.get("/v1/users/selectByUsername/" + value).then(response => {
                        if (response.data.code == 1002) {
                            callback(new Error(response.data.message))
                        } else {
                            callback();
                        }
                    })
                }
            },

            //校验密码
            checkPassword(rule, value, callback) {
                if (value == "") {
                    callback(new Error('密码不能为空'))
                } else {
                    callback();
                }
            },

            //校验昵称
            checkNickname(rule, value, callback) {
                if (value == "") {
                    callback(new Error('昵称不能为空'))
                } else {
                    callback();
                }
            },

            //注册方法
            reg(user) {
                this.$refs[user].validate((valid) => {
                    if (valid) {
                        axios.post("/v1/users/reg", v.user).then(response => {
                            if (response.data.code == 20000) {

                                location.href = "/usermodel/login.html"
                            }
                        })
                    } else {
                        return false;
                    }
                })

                console.log(v.user);
            }
        },
        created(){
            localStorage.clear();
        }
    })
</script>
</html>